//
//  BHMeHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/11.
//

import UIKit
protocol BHMeHeaderViewDelegate {
    // 粉丝、关注和点赞
    func itemViewTypeDidSelect(type: Int)
    
    func showPersonalInfoDidSelect()
    
    func goPersonalUserHome()
}

class BHMeHeaderView: UIView {
    
    var delegate: BHMeHeaderViewDelegate?
    
    @IBOutlet weak var navHeight: NSLayoutConstraint!
    
    @IBOutlet weak var headerImgView: UIImageView!
    
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var fans_view: UIView!
    
    @IBOutlet weak var attension_view: UIView!
    
    @IBOutlet weak var like_view: UIView!
    
    @IBOutlet weak var showPersonalInfoBtn: UIButton!
    
    static func loadFromXib() -> BHMeHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHMeHeaderView", owner: nil, options: nil)
        let view: BHMeHeaderView = viewArray![0] as! BHMeHeaderView
        return view
    }
    
    
    override func awakeFromNib() {
        self.navHeight.constant = UIScreen.begainY
        self.headerImgView.addLayerCorner(30, 0, .color_FC352B)
        
        self.fans_view.addViewAction {[weak self] (_) in
            self?.delegate?.itemViewTypeDidSelect(type: 0)
        }
        
        self.attension_view.addViewAction {[weak self] (_) in
            self?.delegate?.itemViewTypeDidSelect(type: 1)
        }
        
        self.like_view.addViewAction {[weak self] (_) in
            self?.delegate?.itemViewTypeDidSelect(type: 2)
        }
        
        self.headerImgView.addViewAction {[weak self] (_) in
            self?.delegate?.goPersonalUserHome()
        }
    }
    
    @IBAction func showInfoAct(_ sender: UIButton) {
        self.delegate?.showPersonalInfoDidSelect()
    }
    
    
    // 刷新用户信息
    func refreshUser() {
        let fans_label = self.fans_view.viewWithTag(100) as! UILabel
        let attention_label = self.attension_view.viewWithTag(100) as! UILabel
        let like_label = self.like_view.viewWithTag(100) as! UILabel

        
        if BHMeUserInfoModel.shared.token.isEmpty == true {
            self.name_label.text = "未登录"
            self.headerImgView.image = nil
            fans_label.text = "0"
            attention_label.text = "0"
            like_label.text = "0"

        }else {
            self.headerImgView.sd_setImage(with: URL.init(string: BHMeUserInfoModel.shared.avatar), placeholderImage: nil, options: .retryFailed, context: nil)
            self.name_label.text = BHMeUserInfoModel.shared.username
            fans_label.text = "\(BHMeUserInfoModel.shared.fansNum)"
            attention_label.text = "\(BHMeUserInfoModel.shared.attentionNum)"
            like_label.text = "\(BHMeUserInfoModel.shared.myLike)"
        }
       
    }
    
    static var headerHeight: CGFloat {
        get {
            return UIScreen.begainY+138.0
        }
    }
}
